
(function(){

	var ToolBar = G.GClass('Widget', 'ToolBar', G.Base.EventListener);

	ToolBar.prototype.__init = function(actions) {
		this.__Base__EventListener__init();
		this.actions = actions;
		this.GButtons = {};
	}

	ToolBar.prototype.render = function(container) {
		if (typeof(container) == 'string') {
			container = document.getElementById(container);
		}
		this.container = container;

		container.className = "toolbar";

		for (var g in this.actions) {
			var grp = this.actions[g];
			this.GButtons[g] = [];

			for (var i = 0; i < grp.length; i++) {
				var act = grp[i];
				var div = document.createElement('div');
				if (!act.name) {
					div.className = "spliter";
				} else {
					var img = document.createElement('img');
					img.src = act.img;
					img.title = act.title;
					img.name = g + '/' + act.name;
					div.appendChild(img);

					var btn = G(img);
					this.GButtons[g].push(btn);
					btn.bind("onclick", this.__on_click, this);
				}
				container.appendChild(div);
			}
		}
	}

	ToolBar.prototype.__on_click = function(ev) {
		var em = ev.srcElement;
		console.log(em.name);
		this.fire(em.name, em.name);
	}

	ToolBar.prototype.clear = function() {
		this.container.innerHTML = "";
	}

	ToolBar.prototype.remove = function(grp) {
		var btns = this.GButtons[grp] || null;

		if (btns) {
			for (var i in btns) {
				var em = btns[i].em;
				em.parentNode.removeChild(em);
				delete btns[i];
			}
			delete this.GButtons[grp];
		}
	}

	ToolBar.prototype.set_actions = function(actions) {
		this.actions = actions;
	}

	G.DOM.Element.prototype.ToolBar = function(actions) {
		var tb = new ToolBar(actions);
		tb.render(this.em);
		return tb;
	}
})();
